package com.yztc.orangeentertainment.ui.main.channalcontentfragment;

import android.content.Context;
import android.util.Log;

import com.yztc.orangeentertainment.bean.channalbean.ChannalTitle;
import com.yztc.orangeentertainment.bean.channalbean.ChannelTuijian;
import com.yztc.orangeentertainment.bean.channalbean.InfoBean;
import com.yztc.orangeentertainment.bean.channalbean.ListBean;
import com.yztc.orangeentertainment.bean.channalbean.ListBeanGif;
import com.yztc.orangeentertainment.bean.channalbean.ListBeanNormal;
import com.yztc.orangeentertainment.bean.channalbean.ListBeanVideo;
import com.yztc.orangeentertainment.bean.channalbean.ListBeanVote;
import com.yztc.orangeentertainment.config.ChannalUrlConfig;
import com.yztc.orangeentertainment.http.ChannalRetrofitService;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Created by My on 2016/10/5.
 */
public class ChannalContentModle implements ChannalContentContact.Modle {
    private List<ListBeanNormal> normalList;
    private List<ListBeanVote> voteList;
    private List<ListBeanGif> gifList;
    private List<ListBeanVideo> videoList;

    @Override
    public void getChannalContentViewPage(String type, Map<String, String> map, final
    OnLoadChannalContentViewPageListener loadChannalListener, Context context) {
      /*  try {
            Log.i("msg","---------------"+ChannalUrlConfig.CHANNAL_CONTENT_PATH.BASE_URL +type+
                    ChannalUrlConfig
                            .CHANNAL_CONTENT_PATH.Middle_URL + 1 + ChannalUrlConfig
                    .CHANNAL_CONTENT_PATH.END_URL);
            Okhttputils.getRequestEnq(ChannalUrlConfig.CHANNAL_CONTENT_PATH.BASE_URL +type+
                    ChannalUrlConfig
                            .CHANNAL_CONTENT_PATH.Middle_URL + 1 + ChannalUrlConfig.CHANNAL_CONTENT_PATH.END_URL,
                    context, new okhttp3.Callback() {
                        @Override
                        public void onFailure(okhttp3.Call call, IOException e) {

                        }

                        @Override
                        public void onResponse(okhttp3.Call call, okhttp3.Response response) throws IOException {
                            String jsonString=response.body().string();
                            Log.i("msg","-------------"+jsonString);
                            String obj=JSONObject.parseObject(jsonString).getString("data");
                            String obj1=JSONObject.parseObject(jsonString).getString
                                    ("list");
                            normalList=new ArrayList<ListBeanNormal>();
                            normalList= JSONArray.parseArray(obj1,ListBeanNormal.class);
                            videoList=new ArrayList<ListBeanVideo>();
                            videoList=JSONArray.parseArray(obj1,ListBeanVideo.class);
                            gifList=new ArrayList<ListBeanGif>();
                            gifList=JSONArray.parseArray(obj1,ListBeanGif.class);
                            voteList=new ArrayList<ListBeanVote>();
                            voteList=JSONArray.parseArray(obj1,ListBeanVote.class);
                        }
                    });
        } catch (IOException e) {
            e.printStackTrace();
        }*/
        Retrofit retrofit=new Retrofit.Builder()
                .baseUrl(ChannalUrlConfig.PATH.BASE_CHANNALCONTENT_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        Call<ChannelTuijian> call=retrofit.create(ChannalRetrofitService.class)
                .GetChannalContentList(type,map);
        call.enqueue(new Callback<ChannelTuijian>() {
            @Override
            public void onResponse(Call<ChannelTuijian> call, Response<ChannelTuijian> response) {
                if(response.isSuccessful()){
                    List<InfoBean> infoBeanList=response.body().getData().getInfo();
                    ArrayList<ListBean> listBeen= (ArrayList<ListBean>) response.body().getData().getList();
                    loadChannalListener.onSuccess(infoBeanList);
                    loadChannalListener.onSuccessRV(listBeen);
                }else{
                    Log.i("shibai","失败");
                }
            }

            @Override
            public void onFailure(Call<ChannelTuijian> call, Throwable t) {
                loadChannalListener.onFailure(t.getMessage());
            }
        });
    }

    @Override
    public void getChannalContentViewPageFromDb(Call<ChannalTitle> call) {

    }
}
